#include "deviceinfo.h"
#include "ui_deviceinfo.h"
#include "../MainProject/loggermanager.h"

DeviceInfo::DeviceInfo(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::DeviceInfo)
    , deviceId(-1)
    ,common(this)
{
    ui->setupUi(this);
    this->setFixedSize(594, 273); // 设置宽度为844像素，高度为372像素
    common.loadQssfromfile(":/qss/deviceManegerQSS.qss");
    db=  SQLiteDatabase::getInstance();
    connects();
}

Ui::DeviceInfo *DeviceInfo::getUI(){
    return ui;
}

void DeviceInfo::connects(){
    connect(ui->saveBtn,&QPushButton::clicked,this,&DeviceInfo::editDeviceInfo);

}

QJsonObject DeviceInfo::readInfo(){
    QString deviceName=ui->deviceNameEdit->text();
    QString deviceType=ui->deviceTypeEdit->text();
    QString deviceLocator=ui->deviceLocatorEdit->text();
    QString deviceManufacturer=ui->deviceManufacturerEdit->text();
    QString deviceModel=ui->deviceModelEdit->text();
    ui->deviceDateEdit->setDisplayFormat("yyyy-MM-dd");
    QString deviceDate=ui->deviceDateEdit->text();
    qDebug()<<"installation_date:"<<deviceDate;
    QJsonObject newData;
    newData["name"] = deviceName;
    newData["type"] = deviceType;
    newData["location"] = deviceLocator;
    newData["manufacturer"] = deviceManufacturer;
    newData["model"] = deviceModel;
    newData["installation_date"] = deviceDate;
    return newData;
}

void DeviceInfo::onSetDeviceId(int deviceIds){
    deviceId=deviceIds;
}

void DeviceInfo::editDeviceInfo(){
    QJsonObject obj=readInfo();
    QString name=obj["name"].toString();
    QString type=obj["type"].toString();
    QString location=obj["location"].toString();
    QString manufacturer=obj["manufacturer"].toString();
    QString model=obj["model"].toString();
    QString installation_date=obj["installation_date"].toString();

    if(name.isEmpty() ||type.isEmpty() || location.isEmpty() ||
        manufacturer.isEmpty()||model.isEmpty()||installation_date.isEmpty()){
        common.createMessageBox("错误", "输入的设备名称、类型、地址、型号、制造商或安装日期不能为空！");
        return;
    }
    bool matchDate =common.matchRegularExpression("^[12]\\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])$",installation_date);
    if(!matchDate){
        common.createMessageBox("错误", "安装日期格式不正确！");
        return;
    }
    if (deviceId<0){
        common.createMessageBox("警告","设备号错误");
        qDebug()<<"设备号错误";
    }else if(deviceId==0){
        db->create("devices",obj);
        emit sinMainReFlash();
        common.createMessageBox("提示","添加设备成功");
        this->close();
        logText=QString("添加设备成功: name:%1,type:%2,location:%3,manufacturer:%4,model:%5,installation_date:%6")
                      .arg(name).arg(type).arg(location).arg(manufacturer).arg(model).arg(installation_date);
        Logger().logDebug(logText);
    }else if(deviceId>0){
        obj["device_id"] = QString::number(deviceId);
        QString cmd=QString("device_id=%1").arg(deviceId);
        db->update("devices", obj,cmd);
        emit sinMainReFlash();
        common.createMessageBox("提示","修改设备成功");
        this->close();
        logText=QString("修改设备成功: name:%1,type:%2,location:%3,manufacturer:%4,model:%5,installation_date:%6")
                      .arg(name).arg(type).arg(location).arg(manufacturer).arg(model).arg(installation_date);
        Logger().logDebug(logText);
    }
    deviceId=-1;


}

DeviceInfo::~DeviceInfo()
{
    delete ui;
}
